package view.components;

import java.awt.*;

import java.io.*;
import java.util.List;

import javax.swing.*;

import controller.Controller;

import view.Observer;
import view.SLogoView;

/**
 * This canvas contains a turtle canvas which displays turtle's trail It also
 * relates the input textfield with currently selected turtle canvas.
 * 
 * @author Yin, Alex
 * 
 */
@SuppressWarnings("serial")
public class SLogoCanvas extends JPanel

{
    private CommandInput myCommandInput;
    private TurtleCanvas myTurtleCanvas;
    private Controller myController;
    private SLogoTabManager myTab;
    private SLogoHistoryCommands myHistory;
    private static final String SLOGO_CANVAS_SIZE = "TurtleCanvasSize";
    private static final String SLOGO_CANVAS_ERROR = "SLogoCanvasError";

    public SLogoCanvas(Controller controller, SLogoTabManager tab)
            throws IOException {
        myController = controller;
        myTab = tab;

        this.setLayout(new BorderLayout());
        List<Integer> size = SLogoView.getSize(SLOGO_CANVAS_SIZE);
        try {
            setPreferredSize(new Dimension(size.get(0), size.get(1)));
        } catch (Exception e) {
            new ErrorCatcher(SLOGO_CANVAS_ERROR);
        }

        // Create turtle canvas
        myTurtleCanvas = new TurtleCanvas(myController);
        this.add(myTurtleCanvas, BorderLayout.CENTER);

        // Create history window
        myHistory = new SLogoHistoryCommands();
        this.add(new JScrollPane(myHistory), BorderLayout.EAST);

        // Create input line
        myCommandInput = new CommandInput(myController, myTurtleCanvas,
                myHistory, myTab);
        this.add(myCommandInput, BorderLayout.SOUTH);

        this.setVisible(true);

    }

    /**
     * displays the previous commands
     * 
     * @return the history command window
     */
    public SLogoHistoryCommands getHistoryCommandsWindow() {
        return myHistory;
    }

    /**
     * 
     * @return currently selected turtle canvas
     */
    public TurtleCanvas getTurtleCanvas() {
        return myTurtleCanvas;
    }

    /**
     * 
     * @return user input command from myCommandInput
     */
    public String getInput() {
        return myCommandInput.getInput();
    }

    /**
     * 
     * @return the currently selected turtle canvas to be updated
     */
    public Observer getObserver() {
        return myCommandInput;
    }

    /**
     * used in copy/paste
     * 
     * @param str
     */
    public void setInput(String str) {
        myCommandInput.setInput(str);
    }

}
